﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Silverlight.ClientApp.ViewModel;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.Commands;

namespace Galaktika.BI.Silverlight.ClientApp.View
{
    public partial class QueryExecutionViewer : UserControl
    {
        public QueryExecutionViewer()
        {
            InitializeComponent();

            colQueryName = grid.Columns[0] as DataGridTemplateColumn;
            colQueryState = grid.Columns[1] as DataGridTemplateColumn;
            colExecutionTime = grid.Columns[2] as DataGridTextColumn;
            colAction = grid.Columns[3] as DataGridTemplateColumn;
            colCommandId = grid.Columns[4] as DataGridTemplateColumn;
            colResponseSize = grid.Columns[5] as DataGridTextColumn;

            this.UpdateLocalizableResources();
        }

        public void UpdateLocalizableResources()
        {
            colAction.Header = QueryExecutionViewerLocalization.Str_Action;
            colExecutionTime.Header = QueryExecutionViewerLocalization.Str_ExecutionTime;
            colQueryName.Header = QueryExecutionViewerLocalization.Str_QueryName;
            colQueryState.Header = QueryExecutionViewerLocalization.Str_QueryState;
            colCommandId.Header = QueryExecutionViewerLocalization.Str_Details;
            colResponseSize.Header = QueryExecutionViewerLocalization.Str_ResponseSize;
        }

        public QueryExecutionData ViewModel
        {
            get
            {
                return this.Resources["ViewModel"] as QueryExecutionData;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            var qd = grid.SelectedItem as QueryData;
            if (qd != null)
            {
                var taskMgr = App.CurrentApp.GetService<IUserTaskManager>();
                if (taskMgr != null)
                {
                    var caption =
                        string.IsNullOrEmpty(qd.QueryName) ?
                        Localization.AppTask_CancelingQuery :
                        string.Format("{0} '{1}'", Localization.AppTask_CancelingQuery, qd.QueryName);
                    qd.QueryState = Galaktika.BI.Silverlight.Services.QueryExecutionState.Canceling;

                    taskMgr.RunTaskAsync(caption, _ =>
                        {
                            using (var cmd = 
                                CommandDispatcher.CreateAndDispatch(() => CommandBuilder.CreateCancelQueryCommand(qd.Id)))
                            {
                                cmd.Invoke();
                            }
                        });
                }
            }
        }
    }
}
